package com.example.weather;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONObject;

public class MainActivity extends Activity implements View.OnClickListener {
    int flag_right = 0;
    RequestQueue queue = null;
    EditText et_city;
    TextView tv_city, tv_nowtemp, tv_pm25, tv_pm_degree;
    TextView tv_11, tv_12, tv_13, tv_21, tv_22, tv_23, tv_31, tv_32, tv_33, tv_41, tv_42, tv_43;
    Button jump;
    Editable city;
    String des_1, des_2, des_3, des_4, des_5, tipt_zs_1, tipt_zs_2, tipt_zs_3, tipt_zs_4, tipt_zs_5;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        queue = Volley.newRequestQueue(this);
        et_city = (EditText) findViewById(R.id.et_city);
        tv_city = (TextView) findViewById(R.id.id_tv_city);
        tv_pm25 = (TextView) findViewById(R.id.tv_pm25);
        tv_pm_degree = (TextView) findViewById(R.id.tv_pm_degree);
        tv_nowtemp = (TextView) findViewById(R.id.id_tv_nowtemp);
        jump = (Button) findViewById(R.id.btn_jump);
        jump.setOnClickListener(this);

        tv_11 = (TextView) findViewById(R.id.tv_11);
        tv_12 = (TextView) findViewById(R.id.tv_12);
        tv_13 = (TextView) findViewById(R.id.tv_13);

        tv_21 = (TextView) findViewById(R.id.tv_21);
        tv_22 = (TextView) findViewById(R.id.tv_22);
        tv_23 = (TextView) findViewById(R.id.tv_23);

        tv_31 = (TextView) findViewById(R.id.tv_31);
        tv_32 = (TextView) findViewById(R.id.tv_32);
        tv_33 = (TextView) findViewById(R.id.tv_33);

        tv_41 = (TextView) findViewById(R.id.tv_41);
        tv_42 = (TextView) findViewById(R.id.tv_42);
        tv_43 = (TextView) findViewById(R.id.tv_43);
    }

    public void weatherClick(View view) {
        city = et_city.getText();
        flag_right = 1;
        String url = "http://api.map.baidu.com/telematics/v3/weather?location=" + city + "&output=json&ak=vZlRYC39tTuniYzNcX2zrQmZzblZcXwp";
        JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                System.out.println(jsonObject);
                Gson gson = new Gson();
                WeatherBean jiepanxia = gson.fromJson(jsonObject.toString(), WeatherBean.class);
                if (jiepanxia.getError() == 0) {
                     // List<WeatherBean.ResultsBean> jiepanxia;
                    String locate = jiepanxia.getResults().get(0).getCurrentCity();   //城市
                    String date = jiepanxia.getResults().get(0).getWeather_data().get(0).getDate();  //日期
                    String s_pm = jiepanxia.getResults().get(0).getPm25();
                    String empty = "";    //一个空的字符串，坑爹api县级市没有pm2.5指数
                    StringBuffer pm_degree = new StringBuffer();
                    int flag = 0;
                    if (s_pm.equals(empty) == false) {
                        flag = 1;
                        int pmint = Integer.parseInt(s_pm);

                        if (pmint < 50)             //0-50
                            pm_degree.append("空气质量：优");
                        else if (pmint < 100)      //50-100
                            pm_degree.append("空气质量：良");
                        else if (pmint < 150)
                            pm_degree.append("空气质量：轻度污染");
                        else if (pmint < 200)
                            pm_degree.append("空气质量：中度污染");
                        else if (pmint < 300)
                            pm_degree.append("空气质量：重度污染");
                        else
                            pm_degree.append("空气质量：严重污染");
                    }

                    String tv11 = jiepanxia.getResults().get(0).getWeather_data().get(0).getDate();             //相当于一个行列矩阵。具体看实际图片
                    String tv12 = jiepanxia.getResults().get(0).getWeather_data().get(0).getWeather();
                    String tv13 = jiepanxia.getResults().get(0).getWeather_data().get(0).getTemperature();

                    String tv21 = jiepanxia.getResults().get(0).getWeather_data().get(1).getDate();
                    String tv22 = jiepanxia.getResults().get(0).getWeather_data().get(1).getWeather();
                    String tv23 = jiepanxia.getResults().get(0).getWeather_data().get(1).getTemperature();

                    String tv31 = jiepanxia.getResults().get(0).getWeather_data().get(2).getDate();
                    String tv32 = jiepanxia.getResults().get(0).getWeather_data().get(2).getWeather();
                    String tv33 = jiepanxia.getResults().get(0).getWeather_data().get(2).getTemperature();

                    String tv41 = jiepanxia.getResults().get(0).getWeather_data().get(3).getDate();
                    String tv42 = jiepanxia.getResults().get(0).getWeather_data().get(3).getWeather();
                    String tv43 = jiepanxia.getResults().get(0).getWeather_data().get(3).getTemperature();

                    tipt_zs_1 = jiepanxia.getResults().get(0).getIndex().get(0).getTipt() + ":" + jiepanxia.getResults().get(0).getIndex().get(0).getZs();
                    tipt_zs_2 = jiepanxia.getResults().get(0).getIndex().get(1).getTipt() + ":" + jiepanxia.getResults().get(0).getIndex().get(1).getZs();
                    tipt_zs_3 = jiepanxia.getResults().get(0).getIndex().get(2).getTipt() + ":" + jiepanxia.getResults().get(0).getIndex().get(2).getZs();
                    tipt_zs_4 = jiepanxia.getResults().get(0).getIndex().get(3).getTipt() + ":" + jiepanxia.getResults().get(0).getIndex().get(3).getZs();
                    tipt_zs_5 = jiepanxia.getResults().get(0).getIndex().get(4).getTipt() + ":" + jiepanxia.getResults().get(0).getIndex().get(4).getZs();

                    des_1 = jiepanxia.getResults().get(0).getIndex().get(0).getDes();
                    des_2 = jiepanxia.getResults().get(0).getIndex().get(1).getDes();
                    des_3 = jiepanxia.getResults().get(0).getIndex().get(2).getDes();
                    des_4 = jiepanxia.getResults().get(0).getIndex().get(3).getDes();
                    des_5 = jiepanxia.getResults().get(0).getIndex().get(4).getDes();

                    char[] data_1 = tv11.toCharArray();
                    StringBuffer the = new StringBuffer();
                    the.append(data_1[0]);
                    the.append(data_1[1]);
                    the.append("(本日)");

                    char[] datearr = date.toCharArray();
                    StringBuffer now = new StringBuffer();
                    int k = 0;
                    for (int i = 0; i < date.length(); i++) {
                        if (datearr[i] == '：') {
                            k = i;
                            break;
                        }
                    }
                    for (int i = k + 1; datearr[i] != ')'; i++) {
                        now.append(datearr[i]);
                    }
                    tv_11.setText(the.toString());
                    tv_12.setText(tv12);
                    tv_13.setText(tv13);

                    tv_21.setText(tv21);
                    tv_22.setText(tv22);
                    tv_23.setText(tv23);

                    tv_31.setText(tv31);
                    tv_32.setText(tv32);
                    tv_33.setText(tv33);

                    tv_41.setText(tv41);
                    tv_42.setText(tv42);
                    tv_43.setText(tv43);

                    tv_city.setText(locate);
                    tv_nowtemp.setText(now.toString());
                    if (flag == 1)
                        tv_pm25.setText("pm2.5值:" + s_pm);
                    tv_pm_degree.setText(pm_degree.toString());

                } else {
                    Toast.makeText(getApplicationContext(), "城市名称错误，请重试", Toast.LENGTH_SHORT).show();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                System.out.println(volleyError);
            }
        });
        queue.add(request);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btn_jump) {
            if (flag_right == 1) {     //非空
                Intent intent = new Intent(this, second.class);
                intent.putExtra("t1", tipt_zs_1);
                intent.putExtra("t2", tipt_zs_2);
                intent.putExtra("t3", tipt_zs_3);
                intent.putExtra("t4", tipt_zs_4);
                intent.putExtra("t5", tipt_zs_5);

                intent.putExtra("d1", des_1);
                intent.putExtra("d2", des_2);
                intent.putExtra("d3", des_3);
                intent.putExtra("d4", des_4);
                intent.putExtra("d5", des_5);

                startActivity(intent);
            } else {
                Toast.makeText(getApplicationContext(), "请先查询城市天气", Toast.LENGTH_SHORT).show();
            }
        }

    }

}